<template>
  <aside class="sidebar" :style="{ width: sidebarWidth }">
    <div class="class-summary" :class="{ 'compact-mode': !showFullSummary }">
      <div class="summary-content">
        <div class="summary-title">课堂摘要</div>
        <div v-if="loading" class="loading-text">AI摘要加载中...</div>
        <div v-else>{{ displayedText }}</div>
      </div>
      <div class="ai-icon-container">
        <img src="@/assets/picture/AI.png" class="summary-icon" />
      </div>
    </div>
    <ul class="nav-links">
      <li><router-link :to="`/class-management/${classId}/TechNoti`" exact>通知公告</router-link></li>
      <li><router-link :to="`/class-management/${classId}/TechAssignments`" exact>图文作业</router-link></li>
      <li><router-link :to="`/class-management/${classId}/TechExams`" exact>在线考试</router-link></li>
      <li><router-link :to="`/class-management/${classId}/TechOther`" exact>章节设置</router-link></li>
      <li><router-link :to="`/class-management/${classId}/TechSurveys`" exact>问卷调查</router-link></li>
      <li><router-link :to="`/class-management/${classId}/TechData`" exact>数据统计</router-link></li>
      <li><router-link :to="`/class-management/${classId}/TechMaterials`" exact>学生管理</router-link></li>
    </ul>
  </aside>
</template>

<script>
import { ref, computed, onMounted } from 'vue';
import { ClassSummry } from '@/services/api';


export default {
  name: 'T_ClassSidebar',
  props: {
    classId: {
      type: [String, Number],
      required: true
    },
    sidebarWidth: {
      type: String,
      required: true
    }
  },
  setup(props) {
    const fullText = ref('');
    const displayedText = ref('');
    const typingIndex = ref(0);
    const loading = ref(true);
    const error = ref(false);
    const showFullSummary = computed(() => {
      const width = parseInt(props.sidebarWidth) || 240;
      return width > 180;
    });

    const startTyping = () => {
      const interval = setInterval(() => {
        if (typingIndex.value < fullText.value.length) {
          displayedText.value += fullText.value[typingIndex.value];
          typingIndex.value++;
        } else {
          clearInterval(interval);
        }
      }, 50);
    };

    const fetchClassSummary = async () => {
      try {
        const response = await ClassSummry({
          sem_id: localStorage.getItem('selectedClassId'),
          user_id: localStorage.getItem('id')
        });

        fullText.value = response.data?.data || '该课堂暂无摘要';

        // 关键点：重置打字状态
        displayedText.value = '';
        typingIndex.value = 0;

        startTyping();
      } catch (err) {
        console.error('获取课堂摘要失败:', err);
        error.value = true;
      } finally {
        loading.value = false;
      }
    };

    onMounted(() => {
      fetchClassSummary();
    });

    return {
      fullText,
      displayedText,
      typingIndex,
      loading,
      error,
      showFullSummary,
      startTyping,
      fetchClassSummary
    };
  }
};
</script>

<style scoped>
/* 保持原有样式不变 */
.class-summary {
  margin: 20px;
  background: #f7f9fc;
  border: 1px solid #d3e0f1;
  border-radius: 8px;
  padding: 15px;
  font-size: 13px;
  color: #333;
  height: 220px;
  box-sizing: border-box;
  overflow: hidden;
  position: relative;
  transition: all 0.3s ease;
}

.ai-icon-container {
  position: absolute;
  bottom: 5px;
  right: 40px;
  transition: all 0.3s ease;
}
.class-summary.compact-mode {
  width: 80px !important;
  height: 80px !important;
  min-height: 0;
  padding: 8px;
  margin: 10px auto;
}

.class-summary.compact-mode .summary-content {
  display: none;
}

.class-summary.compact-mode .ai-icon-container {
  bottom: 50%;
  right:50%;
  transform: translate(-50%, 50%);
}

.summary-icon {
  width: 26px;
  height: 26px;
  animation: 
    breathGlow 1.5s ease-in-out infinite;
}

.class-summary.compact-mode .summary-icon {
  width: 40px !important;
  height: 40px !important;
}

.summary-title {
  font-weight: bold;
  color: #244382;
  margin-bottom: 10px;
  font-size: 15px;
}

.summary-content {
  line-height: 1.6;
  font-family: 'Courier New', Courier, monospace;
  white-space: pre-wrap;
}
.sidebar {
  width: 240px;
  background-color: #fff;
  box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  height: 100%;
}

.sidebar ul {
  list-style-type: none;
  padding: 0;
  margin-bottom: 10%;
}

.sidebar a {
  text-decoration: none;
  color: #333;
  font-weight: 500;
  display: block;
  padding: 8px 10px;
  transition: background-color 0.3s;
}

.sidebar a:hover {
  background-color: #f0f0f0;
}

.nav-links {
  list-style: none;
}

.nav-links a {
  text-decoration: none;
  padding: 10px;
  padding-left: 20%;
  position: relative;
  font-size: 14px;
}

.nav-links a.router-link-active {
  color: #244382;
  border-left: 3px solid #2E508F;
  margin-left: -3px;
  font-weight: bold;
}
.summary-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.summary-icon {
  width: 26px;
  height: 26px;
  animation: 
    slideIn 0.8s ease-out forwards,
    breathGlow 1.5s ease-in-out infinite 0.8s;
  opacity: 0;
  position: relative;
}

@keyframes slideIn {
  from {
    opacity: 0;
    left: 0%;
    filter: drop-shadow(0 0 8px rgba(72, 206, 239, 0.6));
  }
  to {
    opacity: 1;
    left: 100%;
    filter: drop-shadow(0 0 0 rgba(72, 206, 239, 0));
  }
}

@keyframes breathGlow {
  0% { opacity: 0.8; filter: drop-shadow(0 0 2px rgb(72, 206, 239)); }
  50% { opacity: 1; filter: drop-shadow(0 0 6px rgba(186, 218, 255, 0.8)); }
  100% { opacity: 0.8; filter: drop-shadow(0 0 2px rgba(72, 149, 239, 0.5)); }
}

/* 新增加载和错误状态样式 */
.loading-text, .error-text {
  color: #666;
  font-style: italic;
}

.error-text {
  color: #f56c6c;
}
</style>  